﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using appDataContracts;

namespace appSupplier
{
    /// <summary>
    /// Logique d'interaction pour AppWindow.xaml
    /// </summary>
    public partial class AppWindow : Window
    {
        //public static ISupplier supplierService;
        public static SerializedSupplier supplier;
        public static mySupplierService.SupplierClient supplierService;


        private const string catalogMenuTitle = "Gestion des catalogues";
        private const string productMenuTitle = "Gestion des produits";
        //private const string currentCommandsMenuTitle = "Commandes en cours";
        //private const string oldCommandsMenuTitle = "Historique des commandes";

        #region Ancien constructeur
        //public AppWindow(SerializedSupplier supplier, ISupplier supplierService)
        //{
        //    AppWindow.supplierService = supplierService;
        //    AppWindow.supplier = supplier;

        //    InitializeComponent();

        //    #region Ajout des menus
        //    MenuItem catalogItem = new MenuItem();
        //    catalogItem.Header = catalogMenuTitle;
        //    catalogItem.Background = new System.Windows.Media.LinearGradientBrush(Colors.YellowGreen, Colors.YellowGreen, 90);
        //    catalogItem.MouseEnter += new MouseEventHandler(catalogItem_MouseEnter);
        //    catalogItem.MouseLeave += new MouseEventHandler(catalogItem_MouseLeave);

        //    MenuItem productItem = new MenuItem();
        //    productItem.Header = productMenuTitle;
        //    productItem.Background = new System.Windows.Media.LinearGradientBrush(Colors.YellowGreen, Colors.YellowGreen, 90);
        //    productItem.MouseEnter += new MouseEventHandler(productItem_MouseEnter);
        //    productItem.MouseLeave += new MouseEventHandler(productItem_MouseLeave);


        //    contentMenu.Items.Add(catalogItem);
        //    contentMenu.Items.Add(productItem);
        //    #endregion

        //    #region Masquage des UCs et affichage de l'UC d'accueil
        //    foreach (Control control in this.grid.Children)
        //    {
        //        if (control.GetType().BaseType == typeof(UserControl))
        //        {
        //            control.Visibility = Visibility.Hidden;
        //        }
        //    }
        //    ucCatalogs.Visibility = Visibility.Visible;
        //    this.Title = catalogMenuTitle;
        //    #endregion            

        //} 
        #endregion

        public AppWindow(SerializedSupplier supplier, mySupplierService.SupplierClient supplierService)
        {
            AppWindow.supplierService = supplierService;
            AppWindow.supplier = supplier;

            InitializeComponent();

            #region Ajout des menus
            MenuItem catalogItem = new MenuItem();
            catalogItem.Header = catalogMenuTitle;
            catalogItem.Background = new System.Windows.Media.LinearGradientBrush(Colors.YellowGreen, Colors.YellowGreen, 90);
            catalogItem.MouseEnter += new MouseEventHandler(catalogItem_MouseEnter);
            catalogItem.MouseLeave += new MouseEventHandler(catalogItem_MouseLeave);

            MenuItem productItem = new MenuItem();
            productItem.Header = productMenuTitle;
            productItem.Background = new System.Windows.Media.LinearGradientBrush(Colors.YellowGreen, Colors.YellowGreen, 90);
            productItem.MouseEnter += new MouseEventHandler(productItem_MouseEnter);
            productItem.MouseLeave += new MouseEventHandler(productItem_MouseLeave);


            contentMenu.Items.Add(catalogItem);
            contentMenu.Items.Add(productItem);
            #endregion

            #region Masquage des UCs et affichage de l'UC d'accueil
            foreach (Control control in this.grid.Children)
            {
                if (control.GetType().BaseType == typeof(UserControl))
                {
                    control.Visibility = Visibility.Hidden;
                }
            }
            ucCatalogs.Visibility = Visibility.Visible;
            this.Title = catalogMenuTitle;
            #endregion

        }

        private void Window_Closed(object sender, EventArgs e)
        {
            Application.Current.Shutdown();
        }

        #region Clics du menu
        private void contentMenu_Click(object sender, RoutedEventArgs e)
        {
            #region UCs de Gestion

            #region UC Produits
            if (((MenuItem)e.OriginalSource).Header == productMenuTitle)
            {
                refresh();
                ucProducts.Visibility = Visibility.Visible;
                this.Title = productMenuTitle;
            }
            #endregion

            #region UC Catalogues
            if (((MenuItem)e.OriginalSource).Header == catalogMenuTitle)
            {
                foreach (Control control in this.grid.Children)
                {
                    if (control.GetType().BaseType == typeof(UserControl))
                    {
                        control.Visibility = Visibility.Hidden;
                    }
                }
                ucCatalogs.Visibility = Visibility.Visible;
                this.Title = catalogMenuTitle;
            }
            #endregion

            #endregion
        }

        private void accountMenu_Click(object sender, RoutedEventArgs e)
        {
            #region UC Compte
            refresh();
            ucAccount.Visibility = Visibility.Visible;
            this.Title = "Gestion de votre compte";
            #endregion
        }

        private void commandsMenu_Click(object sender, RoutedEventArgs e)
        {
            #region UC Commandes
            refresh();
            ucOldCommands.Visibility = Visibility.Visible;
            this.Title = "Commandes";
            #endregion
        }

        private void messagesMenu_Click(object sender, RoutedEventArgs e)
        {
            #region UC Messages
            refresh();
            ucMessages.Visibility = Visibility.Visible;
            this.Title = "Messages reçus";
            #endregion
        }
        
        #endregion

        private void refresh()
        {
            foreach (Control control in this.grid.Children)
            {
                if (control.GetType().BaseType == typeof(UserControl))
                {
                    control.Visibility = Visibility.Hidden;
                }
            }
            ucCatalogs.dgCatalog.UnselectAll();
            //ucCatalogs.btDelete.Visibility = Visibility.Hidden;
            //ucCatalogs.btEdit.Visibility = Visibility.Hidden;
            //ucCatalogs.btShowProducts.Visibility = Visibility.Hidden;
            ucProducts.dgProducts.UnselectAll();
            //ucProducts.btDelete.Visibility = Visibility.Hidden;
            //ucProducts.btEdit.Visibility = Visibility.Hidden;
        }

        #region Events couleur du texte du menu
        private void catalogItem_MouseEnter(object sender, MouseEventArgs e)
        {
            imgContent.Source = new BitmapImage(new Uri("/Img/nav-bg-gestion-black.jpg", UriKind.Relative));
            ((MenuItem)sender).Foreground = new System.Windows.Media.LinearGradientBrush(Colors.Black, Colors.Black, 90);
        }

        private void catalogItem_MouseLeave(object sender, MouseEventArgs e)
        {
            imgContent.Source = new BitmapImage(new Uri("/Img/nav-bg-gestion.jpg", UriKind.Relative));
            ((MenuItem)sender).Foreground = new System.Windows.Media.LinearGradientBrush(Colors.White, Colors.White, 90);
        }

        private void productItem_MouseEnter(object sender, MouseEventArgs e)
        {
            imgContent.Source = new BitmapImage(new Uri("/Img/nav-bg-gestion-black.jpg", UriKind.Relative));
            ((MenuItem)sender).Foreground = new System.Windows.Media.LinearGradientBrush(Colors.Black, Colors.Black, 90);
        }

        private void productItem_MouseLeave(object sender, MouseEventArgs e)
        {
            imgContent.Source = new BitmapImage(new Uri("/Img/nav-bg-gestion.jpg", UriKind.Relative));
            ((MenuItem)sender).Foreground = new System.Windows.Media.LinearGradientBrush(Colors.White, Colors.White, 90);
        } 
        #endregion

        #region Events changement d'image menu (mouse over)
        private void contentMenu_MouseEnter(object sender, MouseEventArgs e)
        {
            imgContent.Source = new BitmapImage(new Uri("/Img/nav-bg-gestion-black.jpg", UriKind.Relative));
        }

        private void contentMenu_MouseLeave(object sender, MouseEventArgs e)
        {
            imgContent.Source = new BitmapImage(new Uri("/Img/nav-bg-gestion.jpg", UriKind.Relative));
        }

        private void commandsMenu_MouseEnter(object sender, MouseEventArgs e)
        {
            imgCommands.Source = new BitmapImage(new Uri("/Img/nav-bg-commandes-black.jpg", UriKind.Relative));
        }

        private void commandsMenu_MouseLeave(object sender, MouseEventArgs e)
        {
            imgCommands.Source = new BitmapImage(new Uri("/Img/nav-bg-commandes.jpg", UriKind.Relative));
        }

        private void messagesMenu_MouseEnter(object sender, MouseEventArgs e)
        {
            imgMessages.Source = new BitmapImage(new Uri("/Img/nav-bg-messages-black.jpg", UriKind.Relative));
        }

        private void messagesMenu_MouseLeave(object sender, MouseEventArgs e)
        {
            imgMessages.Source = new BitmapImage(new Uri("/Img/nav-bg-messages.jpg", UriKind.Relative));
        }

        private void accountMenu_MouseEnter(object sender, MouseEventArgs e)
        {
            imgAccount.Source = new BitmapImage(new Uri("/Img/nav-bg-compte-black.jpg", UriKind.Relative));
        }

        private void accountMenu_MouseLeave(object sender, MouseEventArgs e)
        {
            imgAccount.Source = new BitmapImage(new Uri("/Img/nav-bg-compte.jpg", UriKind.Relative));
        }
        #endregion
    }
}